package com.aurora.app.service;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.aurora.app.dao.ChatDao;
import com.aurora.app.entity.Chat;
import com.aurora.base.dao.BaseDao;
import com.aurora.base.dto.Order;
import com.aurora.base.service.BaseService;

@Service
public class ChatService extends BaseService<Chat> {

	@Resource
	private ChatDao baseDao;
	@Override
	public BaseDao<Chat> getBaseDao() {
		// TODO Auto-generated method stub
		return this.baseDao;
	}
	
	public List<Chat> getListByGroupId(String GroupId){
		System.out.println("chat list num"+this.baseDao.list("o.chatGroup.id = ? ", GroupId).size());
		return this.baseDao.list("o.chatGroup.id = ? ", GroupId);
	}
	
//	/**
//	 * 添加一条聊天
//	 * @param chat
//	 */
//	@Transactional
//	public void addChat(Chat chat){
//		this.baseDao.persist(chat);
//	}

	
	/**
	 * 获取组内最近一条聊天记录
	 * @param groupId
	 * @return
	 */
	public Chat getLastChatByGroupId(String groupId){
		Order order= new Order();
		order.put("createTime", Order.DESC);
		List<Chat> list = this.baseDao.list(order, 0, 1, "o.chatGroup.id=?", groupId);
		
		if(list!=null&&!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
}
